<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\yb\RegionModel;
use Illuminate\Http\Request;

class RegionController extends Controller
{

    /**
     * Get a JWT via given credentials.
     *  区域划分提交
     * @return \Illuminate\Http\JsonResponse
     */
    public function region_add(Request $request){

        $points = $request->post('points');
        if(empty($points)){
            return response()->json(['code'=>'201','msg' => '请框选网点区域']);
        }

        $points_data = $request->post('points_data');
        if(empty($points_data)){
            return response()->json(['code'=>'201','msg' => '请框选网点区域']);
        }

        $region_name = $request->post('region_name');
        if(empty($region_name)){
            return response()->json(['code'=>'201','msg' => '请输入区域名称']);
        }

        $region_id = $request->post('region_id');

        if(!empty($region_id)){
            RegionModel::where(['id'=>$region_id])->update([
                'region_name'=>$region_name,
                'points'=>$points,
                'points_data'=>$points_data,
            ]);
        }else{
            RegionModel::insert([
                'region_name'=>$region_name,
                'points'=>$points,
                'points_data'=>$points_data,
                'created_at'=> date('Y-m-d H:i:s',time())
            ]);
        }

        return response()->json(['code'=>'200','msg' => '设置成功']);

    }

    /**
     * Get a JWT via given credentials.
     *  区域划分删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function region_del(Request $request){

        $region_id = $request->post('region_id');
        if(empty($region_id)){
            return response()->json(['code'=>'201','msg' => '请选择区域']);
        }

        RegionModel::where(['id'=>$region_id])->delete();

        return response()->json(['code'=>'200','msg' => '删除成功']);

    }

    /**
     * Get a JWT via given credentials.
     *  区域划分列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function region_listing(Request $request){

        $data = RegionModel::select(['id','region_name','points','points_data','created_at'])->get();

        return response()->json(['code'=>'200','msg' => '设置成功','data'=>$data]);

    }
}
